/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.document.hash.strategy;

import org.dopdf.document.read.DocumentReader;
import org.dopdf.utils.LRUCache;

/**
 * The {@link DocumentHashingStrategy} that is based on the LRU algorithm.  Internally this
 * class uses a simple LinkedHashMap to save the pdf document.
 *
 * @author Deep Shah
 */
public class LRUBasedDocumentHashingStrategy implements DocumentHashingStrategy {

    /** The reference of the LRU cache. */
    private static final LRUCache<String, DocumentReader> cache = new LRUCache<String, DocumentReader>();

    /** The singleton instance of the LRUBasedDocumentHashingStrategy class. */
    private static LRUBasedDocumentHashingStrategy instance;

    /** The reference of identifier generator used by this fetching strategy. */
    private IdentifierGenerator identifierGenerator;

    /**
     * Making the default constructor private so that we could make this class a singleton.
     * @param identifierGenerator the identifier generator to be used.
     */
    private LRUBasedDocumentHashingStrategy(final IdentifierGenerator identifierGenerator) {
        this.identifierGenerator = identifierGenerator;
    }

    /**
     * This method generate the unique ID using the UUID class provided by jdk.
     * {@inheritDoc}
     * @param documentReader
     */
    public String hash(final DocumentReader documentReader) {
        final String key = identifierGenerator.generateUniqueId();
        cache.put(key, documentReader);
        return key;
    }

    /**
     * Returns the document matching the given key.  If the value is not found then null is returned.
     * @param key the key for which we have to find the document.
     * @return the byte array representing the document against the key.
     */
    public DocumentReader unHash(final String key) {
        return cache.get(key);
    }

    /**
     * The method that returns the singleton instance of the LRU cache.
     * @return The instance of LRUBasedDocumentHashingStrategy held by this class.
     */
    public static LRUBasedDocumentHashingStrategy getInstance() {
        if(instance != null) return instance;

        synchronized (LRUBasedDocumentHashingStrategy.class) {
            instance = new LRUBasedDocumentHashingStrategy(new UUIDBasedIdentifierGenerator());
        }
        return instance;
    }
}
